﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pharmacy.Data;
using Pharmacy.Data.Entity;
using System.Transactions;
using Pharmacy.Data.Repositories;
using Pharmacy.Data.Core;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Ultilities;


namespace Pharmacy.Business
{
    public partial class VendorBO : BaseBusiness
    {
        #region Constructors
        public VendorBO(IObjectContext objectContext) : base(objectContext) { }
        public VendorBO() : base() { }
        #endregion

        #region Private Methods


        #endregion

        #region Public Methods
        public Repository<Vendor> ListVendor()
        {
            Repository<Vendor> lstVendor = new Repository<Vendor>(PharmacyContext);
            return lstVendor;

        }

        public void CreateNewVendor(Vendor addedObject)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    VendorRepository lst = new VendorRepository(PharmacyContext);
                    if (addedObject != null)
                    {
                        lst.Add(addedObject);
                        PharmacyContext.SaveChanges();
                        scope.Complete();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        public bool EditVendor(Vendor editObject, out string message)
        {

            try
            {
                VendorRepository lst = new VendorRepository(PharmacyContext);
                if (editObject != null)
                {
                    message = (LockingControl as PessimisticLocking).IsCheckOutByAnotherUser(editObject);
                    if (message.Length == 0)
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            LockingControl.Lock(editObject);
                            PharmacyContext.SaveChanges();
                            scope.Complete();
                            LockingControl.UnLock(editObject);
                            return true;
                        }
                    }
                }
                else
                    message = string.Format(SR.Instance.GetString(Constant.KEY_OBJECTISDELETED), Constant.ENT_VENDOR);
            }
            catch (PessimisticLockingException ex)
            {
                throw new PessimisticLockingException(string.Format(SR.Instance.GetString(Constant.MSG_LOCKBYANOTHERUSER), SR.Instance.GetString(Constant.ENT_VENDORGROUP), editObject.VendorNumber), ex.LockDataInfo);
            }
            return false;
        }

        public bool DeleteVendor(int id, out string messageError)
        {
            Vendor deleteObject = null;
            VendorRepository lst = new VendorRepository(PharmacyContext);
            deleteObject = lst.AsQueryable().First(tmp => tmp.ID == id);
            messageError = string.Empty;

            if (deleteObject != null)
            {
                messageError = (LockingControl as PessimisticLocking).IsCheckOutByAnotherUser(deleteObject);

                using (TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                        if (messageError.Length == 0)
                        {
                            LockingControl.Lock(deleteObject);
                            lst.Delete(deleteObject);
                            PharmacyContext.SaveChanges();
                            LockingControl.UnLock(deleteObject);
                            scope.Complete();
                            return true;
                        }
                    }
                    catch (PessimisticLockingException ex)
                    {
                        throw new PessimisticLockingException(
                            string.Format(SR.Instance.GetString(Constant.MSG_LOCKBYANOTHERUSER),
                            SR.Instance.GetString(Constant.ENT_VENDORGROUP),
                            deleteObject.VendorNumber), ex.LockDataInfo);
                    }
                    return false;
                }
            }
            else
            {
                messageError = string.Format(SR.Instance.GetString(Constant.KEY_OBJECTISDELETED),
                    Constant.ENT_VENDOR);
            }

            return false;
        }
        #endregion
    }
}
